package com.tydic.springboot.excel.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.google.common.collect.Lists;
import com.tydic.springboot.model.Staff;
import com.tydic.springboot.service.StudentSertvice;
import com.tydic.springboot.util.DateUtil;
import com.tydic.springboot.util.HttpServletUtil;
import com.tydic.springboot.util.easyExcel.EasyExcelUtil;
import com.tydic.springboot.util.easyExcel.ExcelSelectedResolve;
import com.tydic.springboot.util.easyExcel.SelectedSheetWriteHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("easyExcel")
public class EasyExcelController {

    @Autowired
    private StudentSertvice studentSertvice;

    @GetMapping("/export")
    public Object export() {
        List<Staff> staffList = studentSertvice.queryUser();
        System.out.println(staffList);
        String filename = "C:\\Users\\wangkl\\Desktop\\user2.xlsx";
        // 设置排除的属性 也可以在数据模型的字段上加@ExcelIgnore注解排除
        Set<String> excludeField = new HashSet<>();
        excludeField.add("userId");
        excludeField.add("userName");
        // 写Excel
        EasyExcel.write(filename, Staff.class)
                .excludeColumnFiledNames(excludeField)
                .sheet("职工信息")
                .doWrite(staffList);
        return staffList;
    }

    @GetMapping("/export2")
    public void download(HttpServletRequest request, HttpServletResponse response) {
        try (OutputStream outputStream = response.getOutputStream()) {
            HttpServletUtil.setResponse(request, response, "用户列表.xlsx");

            List<Staff> staffList = studentSertvice.queryUser();
            //这个实现方式非常简单直接，使用EasyExcel的write方法将查询到的数据进行处理，以流的形式写出即可
            EasyExcel.write(outputStream, Staff.class)//对应的导出实体类
                    .excelType(ExcelTypeEnum.XLSX)//excel文件类型，包括CSV、XLS、XLSX
                    .sheet("职工信息")//导出sheet页名称
                    .doWrite(staffList); //查询获取的数据集合List<T>，转成excel
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/exportTemplate")
    public void exportTemplate(String[] args) {
        File file = new File("C:\\Users\\wangkl\\Desktop\\easyExcel\\test-" + DateUtil.dateFormatSSS() + ".xlsx");
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);

            // 设置排除的属性 也可以在数据模型的字段上加@ExcelIgnore注解排除
            Set<String> excludeField = new HashSet<>();
            excludeField.add("gender");

            EasyExcel.write(fileOutputStream, Staff.class)
                    .excludeColumnFiledNames(excludeField)
                    .sheet("sheet1")
                    .registerWriteHandler(new SelectedSheetWriteHandler(Staff.class))
                    .doWrite(Lists.newArrayList());
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

}
